package com.thirdsdk.umeng;

import android.app.Activity;
import android.content.Context;
import android.graphics.BitmapFactory;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.PopupWindow;

import com.curry.android.http.OrangeDataHandler;
import com.curry.android.http.OrangeErrorHandler;
import com.curry.android.http.VolleyHttpUtil;
import com.curry.android.util.StringUtil;
import com.curry.android.util.ToastHelper;
import com.curry.orangelife.mobile.R;
import com.datatrack.DataTrack;
import com.orangelife.mobile.constants.Constant;
import com.orangelife.mobile.widget.PopupDialog;
import com.orangelife.mobile.widget.SelectPicPopupWindowView;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.bean.SocializeEntity;
import com.umeng.socialize.bean.StatusCode;
import com.umeng.socialize.controller.UMServiceFactory;
import com.umeng.socialize.controller.UMSocialService;
import com.umeng.socialize.controller.listener.SocializeListeners;
import com.umeng.socialize.media.UMImage;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

/**
 * 友盟分享封装
 * Created by YangWei
 * on 2016/5/10.
 *
 * @author yangwei
 */
public class UmShareBiz {
    SelectPicPopupWindowView menuWindow; // 分享弹出框
    PopupDialog popupDialog;
    private String targetUrl = "http://www.com";/*分享url 必须为http连接 */
    private String uMcontent = "";
    private String uMtitle = "";
    @SuppressWarnings("unused")
    private UMImage umengImage;//分享图片
    private final UMSocialService mController = UMServiceFactory
            .getUMSocialService("com.umeng.share");//首先在您的Activity中添加如下成员变量
    private Context mContext;
    private SHARE_MEDIA platform;
    private UMShare share;
    private static final String SHARE_POINTS = Constant.URL_GET_POINTS_SHARE;
    private static final String PRULE_ID = "5";//分享得积分

    /**Activit 名称**/
    final  String MODULE_NAME="/sharePop";

    public UmShareBiz(Context context) {
        this.mContext = context;
        umengImage = new UMImage(context, BitmapFactory.decodeResource(
                context.getResources(), R.drawable.ic_launcher));
        share = UMShare.getInstance();
        share.setUMSS(mController, (Activity) context);
        share.configPlatforms();
    }

    public void share(String uMtitle, String uMcontent, String url, String id) {
        targetUrl = url + id;//特惠分享
        this.uMcontent = uMcontent;
        this.uMtitle = uMtitle;

         popupDialog=new PopupDialog(mContext);
        popupDialog.showDialog(R.layout.dialog_share,itemsOnClick1);


         /* 实例化SelectPicPopupWindow */
//        menuWindow = new SelectPicPopupWindowView(
//                (Activity) mContext, itemsOnClick);
//                /* 显示窗口 */
//        menuWindow.showAtLocation(
//                ((Activity) mContext).findViewById(R.id.main),
//                Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);

        DataTrack.browseTrack(getDataTrack());
    }



    /**
     * 数据采集体不要删除
     * @return
     */
    public Map<String,Object> getDataTrack(){
        Map<String,Object> map=new HashMap<>();
        map.put("UI_HOST", DataTrack.APP_NAME);
        map.put("UI_PATH", MODULE_NAME);
        return map;
    }


    private void UmengShare() {
        share.setShareInfo(uMtitle, uMcontent, targetUrl);
        share.setShare(platform);
        mController.postShare(mContext, platform,
                new SocializeListeners.SnsPostListener() {
                    @Override
                    public void onStart() {
                    }

                    @Override
                    public void onComplete(SHARE_MEDIA platform, int eCode,
                                           SocializeEntity entity) {
                    }
                });
        /* 分享回调 */
        mController.getConfig().registerListener(
                new SocializeListeners.SnsPostListener() {
                    @Override
                    public void onStart() {
                    }

                    @Override
                    public void onComplete(SHARE_MEDIA platform, int eCode,
                                           SocializeEntity entity) {
                        if (eCode == StatusCode.ST_CODE_SUCCESSED) {
                            requestAddPoints();
                        }
                    }
                });
    }

    /**
     * 分享增加积分
     */
    private void requestAddPoints() {
        HashMap<String, Object> map = new HashMap<>();
        map.put("pRuleID", PRULE_ID);
        VolleyHttpUtil.getInstance().postWithCallback(SHARE_POINTS, map, new VolleyHttpUtil.ResponseCallBack() {
            @Override
            public void handleResponse(JSONObject response, int errCode) {
                //Points积分	提示用户获得多少积分 如果返回的积分是0，表示用户已经分享超过3次或者已经签到
                if (OrangeErrorHandler.getInstance().isSuccess(response)) {
                    Map<String, Object> map = OrangeDataHandler.getValueWithEntity(response);
                    String points = String.valueOf(map.get("points"));
                    if (StringUtil.isBlank(points)) {
                        ToastHelper.getInstance().displayToastWithQuickClose("获取积分失败");
                    } else if (points.equals("0")) {
                        ToastHelper.getInstance().displayToastWithQuickClose(
                                "分享福利限每日前三次哦！\n" +
                                        "明天再来吧");
                    } else {
                        ToastHelper.getInstance().displayToastWithQuickClose(
                                "分享成功，奖励橙汁" + points + "ml!橙汁可抵扣现金哦");
                    }

                }
            }
        });
    }


    /**
     * @param type 分享的平台 4 新浪微博 ；5 QQ空间 ；6 QQ； 8 微信 ； 9 微信朋友圈
     * @return 1 微信分享商户;2 微博分享商户;3 QQ分享商户;
     */
    private String whatType(int type) {
        String t = "";
        switch (type) {
            case 4:
                t = "2";
                break;
            case 6:
                t = "3";
                break;
            case 5:
                t = "3";
                break;
            case 8:
                t = "1";
                break;
            case 9:
                t = "4";
            default:
                break;
        }
        return t;
    }


    /**
     * 为弹出窗口实现监听类
     */
    private View.OnClickListener itemsOnClick = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            menuWindow.dismiss();
            switch (v.getId()) {
                case R.id.ib_pengyouquan:
                    platform = SHARE_MEDIA.WEIXIN_CIRCLE;
                    break;
                case R.id.ib_qq:
                    platform = SHARE_MEDIA.QQ;
                    break;
                case R.id.ib_weibo:
                    platform = SHARE_MEDIA.SINA;
                    break;
                case R.id.ib_weixin:
                    platform = SHARE_MEDIA.WEIXIN;
                    break;
                default:
                    break;
            }
            UmengShare();
        }
    };

    private View.OnClickListener itemsOnClick1 = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            popupDialog.dismiss();
            switch (v.getId()) {
                case R.id.ib_pengyouquan:
                    dataTrack("btnShareToWechatCircle");
                    platform = SHARE_MEDIA.WEIXIN_CIRCLE;
                    break;
                case R.id.ib_qq:
                    dataTrack("btnShareToQQ");
                    platform = SHARE_MEDIA.QQ;
                    break;
                case R.id.ib_weibo:
                    dataTrack("btnShareToWeibo");
                    platform = SHARE_MEDIA.SINA;
                    break;
                case R.id.ib_weixin:
                    platform = SHARE_MEDIA.WEIXIN;
                    dataTrack("btnShareToWechatFriend");
                    break;
                default:
                    break;
            }
            UmengShare();
        }
    };


    private void dataTrack(String elmt){
        DataTrack.clickTrack(elmt,null);
    }
}
